<template>
  <div v-loading="exportLoading" class="content">
    ECharts ... ...
    <br />
    <div class="exportRow">
      <el-cascader
        v-model="exportCondition.groupId"
        placeholder="请选择培训组别"
        size="small"
        :options="groupTypeOptions"
        :props="{
          checkStrictly: true,
          value: 'groupId',
          label: 'groupName',
          emitPath: false,
        }"
      ></el-cascader>
      <el-cascader
        v-model="exportCondition.termType"
        placeholder="请选择导出学期"
        size="small"
        :options="termTypeOptions"
      ></el-cascader>
      <el-button
        @click="exportTeacherWorkload()"
        class="sumitButton"
        plain
        size="small"
        type="info"
        >导出教师工作量</el-button
      >
    </div>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      groupTypeOptions: [],
      termTypeOptions: [],
      exportLoading: false,
      exportCondition: {
        groupId: 10000,
        termType: "",
        startDate: "",
        endDate: "",
      },
    };
  },
  created() {
    this.getGroupType();
    this.getTermType();
  },
  mounted() {},
  methods: {
    /**
     * 获取学期信息表
     */
    async getTermType() {
      const res = await this.$axios.get(`${this.HOST}/record/api/term`);
      if (res.data.meta.code == 200) {
        const terms = res.data.data.terms;
        console.log("获取学期信息成功！", terms);
        const termType = [];
        terms.forEach((ele) => {
          let termItem = {
            label: ele.schoolYear,
            children: [
              {
                value: [ele.superior, ele.down],
                label: "全学年",
              },
              {
                value: [ele.superior, ele.mid],
                label: "上学年",
              },
              {
                value: [ele.mid, ele.down],
                label: "下学年",
              },
            ],
          };
          termType.push(termItem);
        });
        this.termTypeOptions = termType;
      }
    },
    /**
     * 获取IT培优组别树
     */
    async getGroupType() {
      if (window.sessionStorage.getItem("groupTree") != null) {
        this.groupTypeOptions = JSON.parse(
          window.sessionStorage.getItem("groupTree")
        );
        console.log("已从本地获得GroupTree！", this.groupTypeOptions);
        return;
      }
      const res = await this.$axios.get(`${this.HOST}/group/api/groupTree`);
      if (res.data.meta.code == 200) {
        let arr = [];
        arr.push(res.data.data.groupTree);
        this.groupTypeOptions = arr;
        console.log("组别获取成功！", this.groupTypeOptions);
        window.sessionStorage.setItem(
          "groupTree",
          JSON.stringify(this.groupTypeOptions)
        );
        // console.log(this.queryInfo.groupId);
      } else if (res.status != 200) {
        this.$message({
          message: "获取失败！",
          type: "error",
        });
      }
    },
    /**
     * 导出教师工作量统计表
     */
    async exportTeacherWorkload() {
      if (
        this.exportCondition.termType == null ||
        this.exportCondition.termType == ""
      ) {
        this.$message({
          message: "请选择导出学期",
          type: "warning",
          showClose: true,
        });
        return;
      }
      this.exportCondition.startDate = this.exportCondition.termType[1][0];
      this.exportCondition.endDate = this.exportCondition.termType[1][1];
      console.log("导出参数", this.exportCondition);
      this.exportLoading = true;
      try {
        const res = await this.$axios.post(
          `${this.HOST}/admin/api/downloadTeacherTrainRecord`,
          this.exportCondition,
          { responseType: "blob" }
        );
        let data = res.data;
        this.showExportDialog = false;
        if (!data) {
          return;
        }
        let url = window.URL.createObjectURL(new Blob([data]));
        let a = document.createElement("a");
        a.style.display = "none";
        a.href = url;
        a.setAttribute("download", "教师工作量统计表.xlsx");
        document.body.appendChild(a);
        a.click(); //执行下载
        window.URL.revokeObjectURL(a.href);
        document.body.removeChild(a);
      } finally {
        this.exportLoading = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  padding-left: 30px;
  padding-right: 30px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .exportRow {
    .sumitButton {
      margin-left: 20px;
    }
  }
}
</style>